﻿#include "taskitemtree.h"


TaskItemTree::TaskItemTree(QWidget *parent) : QTreeWidget(parent)
{
    mscurSelect= QString();
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("system"));

    this->setWindowTitle(tr("TaskItemTree"));
    this->setObjectName("WidgetTaskItemTreeObject");
    //0727新增，增大字体
    this->setStyleSheet("font:12pt \"Calibri\";");

    QStringList labels;
    labels<<tr("Name")<<tr("STime")<<tr("ETime");
    this->setColumnCount(3);
    this->setHeaderLabels(labels);
    this->setColumnWidth(0,200);

    CommonTreeDelegate* delegate = new CommonTreeDelegate(this);
    QList<int> lstInt;
    lstInt.append(0);
    delegate->setColDisable(lstInt);
    this->setItemDelegate(delegate);

    TaskManager::getInstance()->registNotify(this);
    connect(this,SIGNAL(sig_updateTask(QString , QString, QString)),TaskManager::getInstance(),SLOT(slot_updateTask(QString,QString,QString)));
    connect(this,SIGNAL(sig_selectCommand(QString)),TaskManager::getInstance(),SLOT(slot_commandSelect(QString)));
}

void TaskItemTree::setCurSelectTask(const QString& curTaskName)
{
    mscurSelect=curTaskName;
}

void TaskItemTree::mousePressEvent(QMouseEvent *e)
{
    QTreeWidgetItem * pTreeItem = itemAt(e->pos());
    if(NULL != pTreeItem)
    {
        this->setCurrentItem(pTreeItem);
        setCurSelectTask(this->currentItem()->data(0,Qt::WhatsThisRole).toString());
    }
    // 选中某个对象，通知到另外两个显示控件
    if (NULL != pTreeItem && pTreeItem->data(0, Qt::WhatsThisRole).toString().section(":", 0, 0).contains("Command"))
    {
        emit sig_selectCommand(pTreeItem->data(0, Qt::WhatsThisRole).toString());
    }

    if(e->button() == Qt::RightButton)
    {
        QMenu menu;
        menu.addAction(QIcon(),tr("LoadSolution"),this,SLOT(slot_loadSolution()));

        menu.addAction(QIcon(),tr("AddTask"),this,SLOT(slot_addTask()));

        menu.addAction(QIcon(),tr("SaveSolution"),this,SLOT(slot_saveSolution()));

        menu.addAction(QIcon(),tr("CalEfficency"),this,SLOT(slot_CalEfficency()));

        menu.addAction(QIcon(),tr("CalEfficencyfastpoint"),this,SLOT(slot_Calnotfastpoint()));
        if(NULL != pTreeItem)
        {
            if(pTreeItem->data(0,Qt::WhatsThisRole).toString().section(":",0,0).contains("Task",Qt::CaseInsensitive))
            {
                menu.addAction(QIcon(),tr("RemoveTask"),this,SLOT(slot_removeTaskItem()));
                menu.addAction(QIcon(),tr("EditTask"),this,SLOT(slot_editTaskItem()));
                // menu.addAction(QIcon(),tr("AddCommand"),this,SLOT(slot_addCommand()));
            }
            //            else if(pTreeItem->data(0,Qt::WhatsThisRole).toString().section(":",0,0).contains("Command",Qt::CaseInsensitive))
            //            {
            //                menu.addAction(QIcon(),tr("RemoveCommand"),this,SLOT(slot_removeCommandItem()));
            //                menu.addAction(QIcon(),tr("EditCommand"),this,SLOT(slot_editCommandItem()));
            //                menu.addAction(QIcon(),tr("AddScript"),this,SLOT(slot_addScript()));
            //            }
            //            else if(pTreeItem->data(0,Qt::WhatsThisRole).toString().section(":",0,0).contains("Script",Qt::CaseInsensitive))
            //            {
            //                menu.addAction(QIcon(),tr("RemoveScript"),this,SLOT(slot_removeScriptItem()));
            //                menu.addAction(QIcon(),tr("EditScript"),this,SLOT(slot_editScriptItem()));
            //            }
        }
        menu.exec(QCursor::pos());
    }

    

    QTreeWidget::mousePressEvent(e);
}

void TaskItemTree::slot_addTask()
{
    TaskEditDialog dlg;
    dlg.exec();
}

void TaskItemTree::slot_CalEfficency()
{
    EfficencyDialog caldlg(0);
    caldlg.exec();
}

void TaskItemTree::slot_Calnotfastpoint()
{
    EfficencyDialog caldlg(1);
    caldlg.exec();
}

void TaskItemTree::slot_loadSolution()
{
    //清空所有任务
    QVector<TaskItem *> vctTask = TaskManager::getInstance()->allTasks();

    foreach (TaskItem* pTask, vctTask) {
        //QVector<CommandItem *> vctCommand = pTask->allCommands();
        //foreach (CommandItem* pCommand, vctCommand) {
        //if(pCommand->objectName().contains("sPaceScanAuto")/*||pCommand->objectName().contains("sPaceScanAutoPre")*/)
        //{
        TaskManager::getInstance()->removeTask(pTask->objectName());
        //break;
        //}

    }
    //}

    QDir dir(QDir::currentPath());
    dir.cdUp();
    dir.mkdir("data/FY4TaskOptimization");
    dir.cd("data/FY4TaskOptimization");
    QString sFileName = QFileDialog::getOpenFileName(this,tr("Open Solution File"),dir.absolutePath(),tr("File Types (*.xml)"));
    if(sFileName.isEmpty())
        return;

    QFile file(sFileName);
    if(!file.open(QIODevice::ReadOnly))
    {
        return;
    }
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement rootNode = doc.documentElement();
    if(rootNode.isNull())
        return;

    // 0826新增中尺度编辑完善
    QDomElement region=rootNode.firstChildElement("Infos");
    while(!region.isNull())
    {
        QDomElement infoNode=region.firstChildElement("Info");
        TaskManager::getInstance()->mvctregion.clear();
        while(!infoNode.isNull())
        {
            struct regionInfo regioninfo;
            regioninfo.midx=infoNode.attribute("x").toDouble();
            regioninfo.midy=infoNode.attribute("y").toDouble();
            regioninfo.height=infoNode.attribute("height").toDouble();
            regioninfo.widget=infoNode.attribute("width").toDouble();
            regioninfo.cycleNum=infoNode.attribute("cycleNum").toDouble();
            TaskManager::getInstance()->mvctregion.append(regioninfo);
            infoNode=infoNode.nextSiblingElement();
        }
        TaskManager::getInstance()->allregionInfos.append(TaskManager::getInstance()->mvctregion);
        region=region.nextSiblingElement();
    }
    QDomElement commandNode = rootNode.firstChildElement("command");

    double predis=0.0,afterdis=0.0;


    while(!commandNode.isNull())
    {
        QString sType = commandNode.attribute("objname");

        //0526更改FastPoint 为FastPointing 为了data数据向下兼容，做的修改。
        //原有数据包含point不包含pointing
        if(sType.contains("FastPoint")&&(sType.split(" ").at(0)).compare("FastPointing")!=0)
        {

            QString temp=sType.split(" ").at(1);
            sType=sType.split(" ").at(0);
            sType=sType.append("ing").append(" ").append(temp);
        }

        double dSTime = commandNode.attribute("start").toDouble();
        double dETime = commandNode.attribute("end").toDouble();
        QString sID=commandNode.attribute("id");//在增加id处，如果id不为空就用现有idid在哪里用到？

        //0526更改FastPoint 为FastPointing 为了data数据向下兼容，做的修改。
        if(sType.contains("FastPointing")&&!sID.contains("FastPointing"))
        {
            //将现有id加上ing"Command:fb9c:FastPoint 87:527a"
            QString laststr=sID.split(":").at(2);
            QString temp=laststr.split(" ").at(1);
            temp=QString("%1").arg("FastPointing ").append(temp);
            sID.replace(13,13,temp);

        }
        double ewstart=commandNode.attribute("ewstart").toDouble();
        double ewend=commandNode.attribute("ewend").toDouble();
        double nsstart=commandNode.attribute("nsstart").toDouble();
        double nsend=commandNode.attribute("nsend").toDouble();
        if(commandNode.hasAttribute("predis"))
            predis=commandNode.attribute("predis").toDouble();
        if(commandNode.hasAttribute("afterdis"))
            afterdis=commandNode.attribute("afterdis").toDouble();
        int workmode=commandNode.attribute("workmode").toInt();
        double staytime=commandNode.attribute("staytime").toDouble();
        QString targetname=commandNode.attribute("targetname");

        QString sTaskObj = QString("Task:%1:%2").arg(sType.section(" ",0,0)).arg(sID.section(":",1,1));
        TaskItem *pTask = TaskManager::getInstance()->getTask(sTaskObj);
        if(NULL == pTask)
        {
            pTask = TaskManager::getInstance()->addTask(sType.split(" ").at(0));
            QString sObjName = pTask->objectName().section(":",0,1).append(":").append(sID.section(":",1,1));
            pTask->setObjectName(sObjName);
            pTask->setType(sType.split(" ").at(0));
        }

        CommandItem* pCommandItem=pTask->addCommand(sType);

        pCommandItem->setObjectName(sID);

        CommandItem::FastTargetPos  pos;
        pos.mdTargetPosDuration=staytime;
        pos.miTargetPosWorkMode=targetname;
        pCommandItem->setWorkMode((CommandItem::CmdWorkMode)workmode);
        pCommandItem->setFastPosMode(pos);
        pCommandItem->setValue(ScriptItem::Time_Start,dSTime);
        pCommandItem->setValue(ScriptItem::Time_End,dETime);
        pCommandItem->setValue(ScriptItem::Pos_EWEnd,ewend);
        pCommandItem->setValue(ScriptItem::Pos_EWStart,ewstart);
        pCommandItem->setValue(ScriptItem::Pos_NSStart,nsstart);
        pCommandItem->setValue(ScriptItem::Pos_NSEnd,nsend);
        pCommandItem->setValue(ScriptItem::Pos_realEWstart,ewstart-predis);
        pCommandItem->setValue(ScriptItem::Pos_realEWend,ewend+afterdis);
        commandNode = commandNode.nextSiblingElement("command");
    }
    TaskManager::getInstance()->slot_updateTask(QString(),QString(),QString());
}

/**
 * @brief TaskItemTree::slot_saveSolution
 * 输出的是脚本，所以采用指令输出，按照时间顺序输出
 */
void TaskItemTree::slot_saveSolution()
{
    QDir dir(QDir::currentPath());
    dir.cdUp();
    dir.mkdir("data/FY4TaskOptimization");
    dir.cd("data/FY4TaskOptimization");
    QString sFileName = QFileDialog::getSaveFileName(this,tr("Save Solution File"),dir.absolutePath(),tr("File Types (*.xml)"));
    if(sFileName.isEmpty())
        return;

    QDomDocument doc;
    QDomElement rootNode = doc.createElement("root");

    // 取出所有的指令，采用排序算法，对指令按照开始时间进行排序，
    QVector<CommandItem *> vctCommands = TaskManager::getInstance()->getSortCommand();

    foreach (CommandItem* pCommand, vctCommands) {


        QDomElement cmdNode = doc.createElement("command");
        cmdNode.setAttribute("id",pCommand->objectName());
        QString ChineseName=pCommand->getName().split(" ").at(0);
        QString name=pCommand->getName().split(" ").at(1);

        if(ChineseName=="FastPointing")
        {
            ChineseName = tr("CFastPoint");
        }
        else if(ChineseName=="ChinascanNormal")
        {
            ChineseName = tr("CChinaScan");
        }
        else if(ChineseName=="FulldiskNormal")
        {
            ChineseName = tr("CFullDisk");
        }
        else if(ChineseName=="InfraredBackground")
        {
            ChineseName = tr("CRedBackground");
        }
        else if(ChineseName=="BlackbodyScan")
        {
            ChineseName = tr("CBlackBody");
        }
        else if(ChineseName=="sPaceScan")
        {
            ChineseName = tr("CsPaceScan");
        }
        else if(ChineseName=="MidRegionScan")
        {
            ChineseName = tr("CMidRegion");
        }
        else if(ChineseName=="StarScan")
        {
            ChineseName=tr("CStarScan");
        }
        cmdNode.setAttribute("objname",pCommand->getName());
        cmdNode.setAttribute("name",ChineseName.append(" ").append(name));
        //name后面加上行号，id    command：n
        cmdNode.setAttribute("workmode",pCommand->getWorkMode());
        ////cmdNode.setAttribute("targetmode",pCommand->getFastPosMode().miTargetPosWorkMode/*.split(":").at(1)*/);
        cmdNode.setAttribute("staytime",pCommand->getFastPosMode().mdTargetPosDuration);
        cmdNode.setAttribute("start",pCommand->getValue(ScriptItem::Time_Start));
        cmdNode.setAttribute("end",pCommand->getValue(ScriptItem::Time_End));
        cmdNode.setAttribute("ewstart",pCommand->getValue(ScriptItem::Pos_EWStart));
        cmdNode.setAttribute("ewend",pCommand->getValue(ScriptItem::Pos_EWEnd));
        cmdNode.setAttribute("nsstart",pCommand->getValue(ScriptItem::Pos_NSStart));
        cmdNode.setAttribute("nsend",pCommand->getValue(ScriptItem::Pos_NSEnd));
        if(pCommand->getWorkMode()==CommandItem::Mode_SlideScan)
        {
            //qDebug()<<pCommand->getValue(ScriptItem::Pos_realEWstart)<<pCommand->getValue(ScriptItem::Pos_realEWend);
            //qDebug()<<pCommand->getValue(ScriptItem::Pos_EWStart)<<pCommand->getValue(ScriptItem::Pos_EWEnd);
            cmdNode.setAttribute("predis",QString::number(qAbs(pCommand->getValue(ScriptItem::Pos_realEWstart)-pCommand->getValue(ScriptItem::Pos_EWStart)),'f',6));
            cmdNode.setAttribute("afterdis",QString::number(qAbs(pCommand->getValue(ScriptItem::Pos_EWEnd)-pCommand->getValue(ScriptItem::Pos_realEWend)),'f',6));
        }
        else
        {
            cmdNode.setAttribute("predis",0.0);
            cmdNode.setAttribute("afterdis",0.0);
        }
        //cmdNode.setAttribute("pretime",pretime);
        //cmdNode.setAttribute("afttime",afttime);

        rootNode.appendChild(cmdNode);
    }
    doc.appendChild(rootNode);

    //0826新增中尺度编辑完善
    //1009修改为遍历任务，有几个中尺度的信息，就加几个infos，然后读取的时候，依次读取====

    for(int num=0;num<TaskManager::getInstance()->allregionInfos.size();++num)
    {
        QDomElement regionNode1=doc.createElement(QString("Infos")/*.arg(num+1)*/);
        for(int i=0;i<TaskManager::getInstance()->allregionInfos.at(num).size();++i)
        {
            QDomElement regioninfos=doc.createElement("Info");
            regioninfos.setAttribute(QString("x"),TaskManager::getInstance()->allregionInfos.at(num).at(i).midx);
            regioninfos.setAttribute(QString("y"),TaskManager::getInstance()->allregionInfos.at(num).at(i).midy);
            regioninfos.setAttribute(QString("height"),TaskManager::getInstance()->allregionInfos.at(num).at(i).height);
            regioninfos.setAttribute(QString("width"),TaskManager::getInstance()->allregionInfos.at(num).at(i).widget);
            regioninfos.setAttribute(QString("cycleNum"),TaskManager::getInstance()->allregionInfos.at(num).at(i).cycleNum);
            regionNode1.appendChild(regioninfos);
        }
        rootNode.appendChild(regionNode1);
    }

    QString sxmltitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
    QFile file(sFileName);
    if(!file.open(QIODevice::WriteOnly))
        return;

    sxmltitle.append(doc.toString());
    file.write(sxmltitle.toUtf8());

    file.close();
}

void TaskItemTree::slot_editTaskItem()
{
    midregions.clear();

    QTreeWidgetItem* pCurrentItem = this->currentItem();

    if(NULL == pCurrentItem)
    {
        return;
    }

    QString sTaskObj = pCurrentItem->data(0,Qt::WhatsThisRole).toString();

    for(int i=0;i<this->topLevelItemCount();++i)
    {
        QTreeWidgetItem *tempitem=topLevelItem(i);
        if(tempitem->data(0,Qt::WhatsThisRole).toString().contains("MidRegionScan"))
        {
            midregions.append(tempitem);
            if(tempitem->data(0,Qt::WhatsThisRole).toString()==sTaskObj)
            {
                //当前被点击的是第几个item，找当前被点击的是第几个midregion
                TaskManager::getInstance()->setcurMidNum(midregions.size());
                break;
            }
        }

    }
    TaskEditDialog dlg(sTaskObj);

    QVector<TaskItem*> vctTask = TaskManager::getInstance()->allTasks();
    if(vctTask.isEmpty())
        return;
    foreach(TaskItem *pTask,vctTask)
    {

        if(pTask->objectName()==mscurSelect)
            dlg.setTaskItem(pTask);
    }

    if(dlg.exec() == QDialog::Accepted)
    {
        return;
    }
}
void TaskItemTree::slot_removeTaskItem()
{
    QTreeWidgetItem *pCurItem=this->currentItem();
    if(NULL==pCurItem)
        return;
    QString pTaskName=pCurItem->data(0,Qt::WhatsThisRole).toString();
    if(pTaskName.contains("MidRegionScan"))
    {
        for(int i=0;i<this->topLevelItemCount();++i)
        {
            QTreeWidgetItem *tempitem=topLevelItem(i);
            if(tempitem->data(0,Qt::WhatsThisRole).toString().contains("MidRegionScan"))
            {
                midregions.append(tempitem);
                if(tempitem->data(0,Qt::WhatsThisRole).toString()==pTaskName)
                {
                    //当前被点击的是第几个item，找当前被点击的是第几个midregion
                    TaskManager::getInstance()->removecurMidNum(midregions.size());
                    break;
                }
            }

        }
    }
    TaskManager::getInstance()->removeTask(pTaskName);
}

void TaskItemTree::updateTask(const QString &taskObj, const QString &cmdObj, const QString &scriptObj)
{
    if(taskObj.isEmpty())
    {
        //如果这个task不存在子节点，可以理解为这个task是无效节点
        QVector<TaskItem*> vctTask = TaskManager::getInstance()->allTasks();
        foreach (TaskItem* ptask, vctTask) {

            QTreeWidgetItem* ptopItem = isExistTopItem(ptask->objectName());
            if(NULL == ptopItem)
            {
                QStringList lstValue;
                lstValue.append(ptask->getName());
                lstValue.append(QString::number(ptask->getValue(ScriptItem::Time_Start),'f',6));
                lstValue.append(QString::number(ptask->getValue(ScriptItem::Time_End),'f',6));

                ptopItem = new QTreeWidgetItem(lstValue);
                //0727新增，设置背景颜色
                StyleItem *pstyle=TaskManager::getInstance()->getStyle(lstValue.at(0));
                ptopItem->setBackground(0,QBrush(pstyle->getBrushColor()));
                ptopItem->setTextColor(0,0x00ffffff^pstyle->getBrushColor());
                ptopItem->setData(0,Qt::WhatsThisRole,ptask->objectName());
                ptopItem->setData(1,Qt::NoItemFlags,QString());
                ptopItem->setData(2,Qt::NoItemFlags,QString());

                this->addTopLevelItem(ptopItem);
            }

            QVector<CommandItem *> vctCommand = ptask->allCommands();
            if(vctCommand.isEmpty())
            {
                TaskManager::getInstance()->removeTask(ptask->objectName());
            }
            foreach (CommandItem* pCommand, vctCommand) {
                QTreeWidgetItem* pChildItem = getChildTreeItem(ptopItem,pCommand->objectName());
                if(NULL == pChildItem)
                {
                    QStringList lstValue;
                    lstValue.append(pCommand->getName());
                    lstValue.append(QString::number(pCommand->getValue(ScriptItem::Time_Start),'f',6));
                    lstValue.append(QString::number(pCommand->getValue(ScriptItem::Time_End),'f',6));
                    pChildItem = new QTreeWidgetItem(lstValue);
                    pChildItem->setData(0,Qt::WhatsThisRole,pCommand->objectName());

                    ptopItem->addChild(pChildItem);
                }
            }
        }
    }
    else if(cmdObj.isEmpty())
    {
        TaskItem* pcurTask = TaskManager::getInstance()->getTask(taskObj);
        if(NULL == pcurTask)
            return;

        QTreeWidgetItem* ptopItem = isExistTopItem(pcurTask->objectName());
        if(NULL == ptopItem)
        {
            QStringList lstValue;
            lstValue.append(pcurTask->getName());
            lstValue.append(QString::number(pcurTask->getValue(ScriptItem::Time_Start),'f',6));
            lstValue.append(QString::number(pcurTask->getValue(ScriptItem::Time_End),'f',6));
            ptopItem = new QTreeWidgetItem(lstValue);
            StyleItem *pstyle=TaskManager::getInstance()->getStyle(lstValue.at(0));
            ptopItem->setBackground(0,QBrush(pstyle->getBrushColor()));
            ptopItem->setTextColor(0,0x00ffffff^pstyle->getBrushColor());
            ptopItem->setData(0,Qt::WhatsThisRole,pcurTask->objectName());
            ptopItem->setData(1,Qt::NoItemFlags,QString());
            ptopItem->setData(2,Qt::NoItemFlags,QString());
            this->addTopLevelItem(ptopItem);
        }

        QVector<CommandItem *> vctCommand = pcurTask->allCommands();
        foreach (CommandItem* pCommand, vctCommand) {


            QTreeWidgetItem* pChildItem = getChildTreeItem(ptopItem,pCommand->objectName());
            if(NULL == pChildItem)
            {
                QStringList lstValue;
                lstValue.append(pCommand->getName());
                lstValue.append(QString::number(pCommand->getValue(ScriptItem::Time_Start),'f',6));
                lstValue.append(QString::number(pCommand->getValue(ScriptItem::Time_End),'f',6));
                pChildItem = new QTreeWidgetItem(lstValue);
                pChildItem->setData(0,Qt::WhatsThisRole,pCommand->objectName());
                ptopItem->addChild(pChildItem);
            }
            else
            {
                pChildItem->setData(0,Qt::WhatsThisRole,pCommand->objectName());
                pChildItem->setData(1,Qt::DisplayRole,QString::number(pCommand->getValue(ScriptItem::Time_Start),'f',6));
                pChildItem->setData(2,Qt::DisplayRole,QString::number(pCommand->getValue(ScriptItem::Time_End),'f',6));
            }
        }

    }
    else
    {
        TaskItem* pcurTask = TaskManager::getInstance()->getTask(taskObj);
        if(NULL == pcurTask)
            return;

        CommandItem* pCurCommand = pcurTask->getCommand(cmdObj);

        if(NULL == pCurCommand)
            return;

        QTreeWidgetItem* ptopItem = isExistTopItem(pcurTask->objectName());
        if(NULL == ptopItem)
        {
            QStringList lstValue;
            lstValue.append(pcurTask->getName());
            lstValue.append(QString::number(pcurTask->getValue(ScriptItem::Time_Start),'f',6));
            lstValue.append(QString::number(pcurTask->getValue(ScriptItem::Time_End),'f',6));
            ptopItem = new QTreeWidgetItem(lstValue);
            StyleItem *pstyle=TaskManager::getInstance()->getStyle(lstValue.at(0));
            ptopItem->setBackground(0,QBrush(pstyle->getBrushColor()));
            ptopItem->setTextColor(0,0x00ffffff^pstyle->getBrushColor());
            ptopItem->setData(0,Qt::WhatsThisRole,pcurTask->objectName());
            ptopItem->setData(1,Qt::NoItemFlags,QString());
            ptopItem->setData(2,Qt::NoItemFlags,QString());
            this->addTopLevelItem(ptopItem);
        }


        QTreeWidgetItem* pChildItem = getChildTreeItem(ptopItem,pCurCommand->objectName());
        if(NULL == pChildItem)
        {
            QStringList lstValue;
            lstValue.append(pCurCommand->getName());
            lstValue.append(QString::number(pCurCommand->getValue(ScriptItem::Time_Start),'f',6));
            lstValue.append(QString::number(pCurCommand->getValue(ScriptItem::Time_End),'f',6));
            pChildItem = new QTreeWidgetItem(lstValue);
            pChildItem->setData(0,Qt::WhatsThisRole,pCurCommand->objectName());
            ptopItem->addChild(pChildItem);
        }
    }
}

void TaskItemTree::commandSelect(const QString &cmdObj)
{
    for(int i=0; i<this->topLevelItemCount(); ++i)
    {
        QTreeWidgetItem* pTopItem = this->topLevelItem(i);
        if(cmdObj.contains(pTopItem->data(0,Qt::WhatsThisRole).toString().section(":",-1,-1),Qt::CaseInsensitive))
        {
            for(int j=0; j<pTopItem->childCount(); ++j)
            {
                QTreeWidgetItem* pChildItem = pTopItem->child(j);
                if(pChildItem->data(0,Qt::WhatsThisRole).toString().contains(cmdObj,Qt::CaseInsensitive))
                {
                    this->setCurrentItem(pChildItem);
                    break;
                }
                else
                {
                    continue;
                }
            }
        }
        else
        {
            continue;
        }
    }
}

void TaskItemTree::removeTask(const QString &sTaskObj)
{

    for(int i=0;i<this->topLevelItemCount();++i)
    {
        if(this->topLevelItem(i)->data(0,Qt::WhatsThisRole).toString()==sTaskObj)
        {
            QTreeWidgetItem * pTopItem=this->takeTopLevelItem(i);
            delete pTopItem;
            pTopItem=NULL;
            break;
        }
    }
}
QTreeWidgetItem* TaskItemTree::isExistTopItem(QString sItemText, int role)
{
    for(int i=0; i<this->topLevelItemCount(); ++i)
    {
        if(this->topLevelItem(i)->data(0,role) == sItemText)
        {
            return this->topLevelItem(i);
        }
        else
        {
            continue;
        }
    }
    return NULL;
}

QTreeWidgetItem *TaskItemTree::getChildTreeItem(QTreeWidgetItem * parent,QString sItemText, int role)
{
    for(int i=0; i<parent->childCount(); ++i)
    {
        if(parent->child(i)->data(0,role) == sItemText)
        {
            return parent->child(i);
        }
        else
        {
            continue;
        }
    }
    return NULL;
}
